#!/usr/bin/env python
# -*- coding: utf-8 -*-

from core.utils.singleton import singleton
from xml.etree.ElementTree import ElementTree
from core.ide.datamangager import DataMangager
import wx
import os

DEFAULT_THEME='druid'
#TODO revoir la struture
#TODO ?limener theme, combine les xml pour un seul theme avec plusieurs source?

THEME_LIB={
"ICONE16":"icone_16"
}

class ThemeLib:
    ICONE16="icone_16"


@singleton
class ThemesManager():
    
    
    
    def __init__(self):
        self._theme=None
        self._themes={}
        self.data=DataMangager().getData("Druid","Theme",0,["druid"],"Themes")
        
        
    def getListThemes(self):
        self._themes
        
    def addLibrary(self):
        pass
    
    def addTheme(self,id):
        
        theme=Theme(id)
        if not self._theme:
            self._theme=theme
        if not self._themes.has_key(id):
            self.data.options.append(id)
        self._themes[id]=theme
        
    
    def getTheme(self):
        pass
    
    def getDefaultTheme(self):
        return self._theme
    
    def setDefaultTheme(self):
        pass


#Todo ne pas conserver le xml
class Theme:
    
    TYPE_WX ='wx'
    TYPE_DRUID ='druid'
    
    def __init__(self, id_,path=None):
        if not path:
            path=os.path.abspath('extensions/themes/'+id_)
        self._xml = ElementTree(None,path+'/theme.xml')
        self._lib={}
        self._wxlib={}
        self._wxLibId={}
        self._dir=path
        
        self._readHeader()
        #self._readLib("icone_16",self.TYPE_WX)
    
    def _readHeader(self):
        self.name=self._xml.findtext("head/name")
        
    
    def _readLib(self,libName,type_):
        node=self._xml.find("lib[@name='"+libName+"']")
        if type_==self.TYPE_WX:
            self._wxLibId[libName]={}
            wxLib=self._createWxLib(node,libName)
            self._wxlib[libName]=wxLib
        else:
            lib=self._createLib(node)
            self._lib[libName]=lib

    
    def _createWxLib(self,xml,libName):
        size=xml.attrib['size'].split(',')
        wxLib=wx.ImageList(int(size[0]),int(size[1]))
        imgs=xml.findall('img')
        cpt=0;
        for img in imgs:
            bmp = wx.Image(self._dir+'/'+img.text).ConvertToBitmap()
            self._wxLibId[libName][img.attrib['name']]=cpt
            wxLib.Add(bmp)
            cpt+=1
        return wxLib

    #Todo créer une libraire si n'existe pas et éviter d'écraser
    def getImage(self,imgName,libName=None):
        if libName==None:
            child=self._xml.find('lib/img[@name="'+imgName+'"]')
            return wx.Image(self._dir+'/'+child.text).ConvertToBitmap()
        elif self._lib.has_key(libName):
            return self._lib[libName].getImage(imgName)
        else:
            child=self._xml.find('lib[@name="'+libName+'"]/img[@name="'+imgName+'"]')
            return wx.Image(self._dir+'/'+child.text).ConvertToBitmap()
    
    def getImageURL(self,imgName,libName=None):
        if libName==None:
            child=self._xml.find('lib/img[@name="'+imgName+'"]')
            return self._dir+'/'+child.text
        else:
            child=self._xml.find('lib[@name="'+libName+'"]/img[@name="'+imgName+'"]')
            return self._dir+'/'+child.text
        return None
            
    def getImages(self,libName):
        node=self._xml.find("lib[@name='"+libName+"']")
        imgs=node.findall('img')
        ret={}
        for img in imgs:
            bmp = wx.Image(self._dir+img.text).ConvertToBitmap()
            ret[img.attrib['name']]=bmp
        return ret
    
    def getWxLib(self,name):
        if not self._wxlib.has_key(name):
            self._readLib(name,self.TYPE_WX)
        return self._wxlib[name]
        
    def getWxIdImage(self,libName,imgName):
        if not self._wxlib.has_key(libName):
            self._readLib(libName,self.TYPE_WX)
        
        try:
            return self._wxLibId[libName][imgName]
        except:
            return None
        
    
    def removeLib(self,name):
        if self._wxlib.has_key(name):
            del self._wxlib[name]
        if self._lib.has_key(name):
            del self._lib[name]
            
    def destroy(self):
        pass